package com.yoga.manager.app.impl;

import com.yoga.common.appexception.AppErrorCodeEnum;
import com.yoga.common.appexception.AppException;
import com.yoga.common.bo.AboutClassRecordBO;
import com.yoga.common.bo.ClassTableBO;
import com.yoga.common.bo.PlainVipBO;
import com.yoga.common.bo.YogaUserBO;
import com.yoga.common.page.PageBO;
import com.yoga.common.qto.AboutClassRecordQTO;
import com.yoga.dao.dao.AboutClassRecordDao;
import com.yoga.dao.dao.ClassTableDao;
import com.yoga.dao.dao.PlainVipDao;
import com.yoga.dao.dao.YogaUserDao;
import com.yoga.dao.domain.AboutClassRecordDO;
import com.yoga.dao.domain.ClassTableDO;
import com.yoga.manager.app.AppAboutClassRecordManager;
import com.yoga.manager.app.AppClassTableManager;
import com.yoga.manager.beanUtil.AboutClassRecordBeanUtil;
import com.yoga.manager.uitl.DateStyle;
import com.yoga.manager.uitl.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created by hdx on 2020/10/3.
 */
@Component
public class AppAboutClassRecordManagerImpl implements AppAboutClassRecordManager {
    Logger logger = LoggerFactory.getLogger(AppAboutClassRecordManagerImpl.class);

    @Resource
    private AboutClassRecordDao aboutClassRecordDao;

    @Resource
    private ClassTableDao classTableDao;

    @Resource
    private AppClassTableManager appClassTableManager;

    @Resource
    private PlainVipDao plainVipDao;

    @Resource
    private YogaUserDao yogaUserDao;

    @Override
    public AboutClassRecordBO addData(YogaUserBO yogaUserBO, AboutClassRecordBO aboutClassRecordBO) throws AppException {
        if (yogaUserBO == null || aboutClassRecordBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (aboutClassRecordBO.getClassTableId() == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        AboutClassRecordQTO recordQTO = new AboutClassRecordQTO();
        recordQTO.setClassTableId(aboutClassRecordBO.getClassTableId());
        recordQTO.setYogaUserId(yogaUserBO.getYogaUserId());
        AboutClassRecordDO recordDO = aboutClassRecordDao.findRecord(recordQTO);
        if (recordDO != null && recordDO.getRecordStatus() != 2) {
            throw new AppException(AppErrorCodeEnum.ABOUT_EXIST);
        } else if (recordDO != null && recordDO.getRecordStatus() == 2) {
            AboutClassRecordDO update = new AboutClassRecordDO();
            update.setId(recordDO.getId());
            update.setRecordStatus(0);
            update.setAboutDate(DateUtil.dateFormat(new Date(), DateStyle.YYYY_MM_DD));
            update.setDeleteMark(0);
            int num = aboutClassRecordDao.updateById(update);
            if (num <= 0) {
                throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
            }
            yogaUserDao.addClassNum(yogaUserBO.getYogaUserId());
        } else {
            aboutClassRecordBO.setRecordStatus(0);
            aboutClassRecordBO.setYogaUserId(yogaUserBO.getYogaUserId());
            aboutClassRecordBO.setAboutDate(DateUtil.dateFormat(new Date(), DateStyle.YYYY_MM_DD));
            AboutClassRecordDO aboutClassRecordDO = AboutClassRecordBeanUtil.aboutClassRecordBOToDO(aboutClassRecordBO);
            int num = aboutClassRecordDao.insert(aboutClassRecordDO);
            if (num <= 0) {
                throw new AppException(AppErrorCodeEnum.INSERT_DATA_NULL);
            }
            yogaUserDao.addClassNum(yogaUserBO.getYogaUserId());

            aboutClassRecordBO.setAboutClassRecordId(aboutClassRecordDO.getId());

        }

        return aboutClassRecordBO;
    }

    @Override
    public Boolean updateData(AboutClassRecordBO aboutClassRecordBO) throws AppException {
        if (aboutClassRecordBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (aboutClassRecordBO.getAboutClassRecordId() == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        if (aboutClassRecordBO.getYogaUserId() == null) {
            throw new AppException(AppErrorCodeEnum.YOGA_USER_ID_NULL);
        }
        //取消约课
        if (aboutClassRecordBO.getRecordStatus() != null && aboutClassRecordBO.getRecordStatus() == 2) {
            yogaUserDao.addClassNum(aboutClassRecordBO.getYogaUserId());
        }

        int num = aboutClassRecordDao.updateById(AboutClassRecordBeanUtil.aboutClassRecordBOToDO(aboutClassRecordBO));
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return Boolean.TRUE;
    }

    @Override
    public AboutClassRecordBO findByClassTableId(Long yogaUserId, Long classTableId) throws AppException {
        if (yogaUserId == null) {
            throw new AppException(AppErrorCodeEnum.YOGA_USER_ID_NULL);
        }
        if (classTableId == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        AboutClassRecordQTO recordQTO = new AboutClassRecordQTO();
        recordQTO.setClassTableId(classTableId);
        recordQTO.setYogaUserId(yogaUserId);
        AboutClassRecordDO recordDO = aboutClassRecordDao.findRecord(recordQTO);
        return AboutClassRecordBeanUtil.aboutClassRecordDOToBO(recordDO);
    }

    @Override
    public PageBO<AboutClassRecordBO> findForPage(AboutClassRecordQTO aboutClassRecordQTO) throws AppException {
        if (aboutClassRecordQTO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (aboutClassRecordQTO.getYogaUserId() == null) {
            throw new AppException(AppErrorCodeEnum.YOGA_USER_ID_NULL);
        }
        PageBO<AboutClassRecordBO> pageBO = new PageBO<>();
        pageBO.setQuery(aboutClassRecordQTO);
        Integer count = aboutClassRecordDao.findForPageCount(aboutClassRecordQTO);
        if (count == null | count <= 0) {
            return pageBO;
        }
        List<AboutClassRecordBO> boList = AboutClassRecordBeanUtil.doList2BOList(aboutClassRecordDao.findForPage(aboutClassRecordQTO));
        pageBO.setTotalCount(count);
        pageBO.setData(boList);
        if (!CollectionUtils.isEmpty(boList)) {
            List<Long> classTableIds = new ArrayList<>(boList.stream().map(AboutClassRecordBO::getClassTableId).collect(Collectors.toSet()));
            List<ClassTableBO> classTableBOs = appClassTableManager.findByIds(classTableIds);
            if (!CollectionUtils.isEmpty(classTableBOs)) {
                Map<Long, ClassTableBO> classTableBOMap = classTableBOs.stream().collect(Collectors.toMap(ClassTableBO::getClassTableId, Function.identity()));
                if (!CollectionUtils.isEmpty(classTableBOMap)) {
                    boList.stream().filter(s -> s.getClassTableId()!=null && classTableBOMap.containsKey(s.getClassTableId()))
                            .forEach(s -> s.setClassTableBO(classTableBOMap.get(s.getClassTableId())));
                }
            }
        }

        return pageBO;
    }
}
